package com.topmap.service.impl;

import com.topmap.dao.FeiyiDOMapper;
import com.topmap.dataobject.FeiyiDO;
import com.topmap.service.FeiyiService;
import com.topmap.service.model.FeiyiModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/3 11:13
 * @Version 1.0
 */
@Service
public class FeiyiServiceImpl implements FeiyiService{

    //注入mapper
    @Autowired
    private FeiyiDOMapper feiyiDOMapper;

    //注入service
    @Autowired
    private FeiyiService feiyiService;




    //通过非遗名称查询非遗的具体实现方法
    @Override
    public FeiyiModel getFeiyiByTitle(String title) {
        FeiyiDO feiyiDO=feiyiDOMapper.selectByTitle(title);
        if (feiyiDO==null){
            return null;
        }

        //将FeiyiDO转换为FeiyiModel
        FeiyiModel feiyiModel=convertModelFromDataObject(feiyiDO);

        return feiyiModel;
    }


    //通过非遗类型查询非遗的具体实现方法
    @Override
    public List<FeiyiModel> getFeiyiByType(String type) {
        //使用mapper的listProduct方法查询该类型所有的非遗
        List<FeiyiDO> feiyiDOList=feiyiDOMapper.selectByType(type);


        //使用java8的stream api把 feiyiDOList中的每一个都转化出来
        List<FeiyiModel> feiyiModelList=feiyiDOList.stream().map(feiyiDO -> {
            //map成feiyiModel
            FeiyiModel feiyiModel=this.convertModelFromDataObject(feiyiDO);
            return feiyiModel;
        }).collect(Collectors.toList());
        //返回出去productModelList
        return feiyiModelList;
    }

    //查询所有非遗信息
    @Override
    public List<FeiyiModel> listFeiyi() {
        //使用mapper的listProduct方法查询所有的商品dataobject
        List<FeiyiDO> feiyiDOList=feiyiDOMapper.listFeiyi();

        //使用java8的stream api把 feiyiDOList中的每一个都转化出来
        List<FeiyiModel> feiyiModelList=feiyiDOList.stream().map(feiyiDO -> {
            //map成feiyiModel
            FeiyiModel feiyiModel=this.convertModelFromDataObject(feiyiDO);
            return feiyiModel;
        }).collect(Collectors.toList());
        //返回出去productModelList
        return feiyiModelList;
    }


    //通过非遗id查询非遗的具体实现方法
    @Override
    public FeiyiModel getFeiyiById(Integer id) {
        FeiyiDO feiyiDO=feiyiDOMapper.selectById(id);
        if(feiyiDO==null){
            return null;
        }

        //将FeiyiDO转换为FeiyiModel
        FeiyiModel feiyiModel=convertModelFromDataObject(feiyiDO);

        return feiyiModel;
    }


    //把feiyiDO转换为feiyiModel的方法
    private FeiyiModel convertModelFromDataObject(FeiyiDO feiyiDO) {
        FeiyiModel feiyiModel=new FeiyiModel();

        //copy FeiyiDO
        BeanUtils.copyProperties(feiyiDO,feiyiModel);

        return feiyiModel;



    }
}
